import requests
from app.config import GAODE_API


def get_city_by_latlng(lat, lng, key):
    lat = round(lat, 6)
    lng = round(lng , 6)
    print(lat, lng)
    url = f"https://restapi.amap.com/v3/geocode/regeo?output=json&location={lng},{lat}&key={key}"
    response = requests.get(url)
    result = response.json()
    if result['status'] == '1':
        return result
        # return result['regeocode']['addressComponent']['city']
    else:
        print(f"无法获取城市信息，请检查经纬度或API Key是否正确：{lat}, {lng}")
        return None

# 使用示例
key = GAODE_API
latitude = 37.376813386464896
longitude = 115.41164692230367
city = get_city_by_latlng(latitude, longitude, key)
print(city)